<script setup lang="ts">
import { ref } from 'vue';
import { useRouter } from 'vue-router';

const phone = ref('');
const password = ref('');
const agree = ref(false);
const phoneError = ref('');
const passwordError = ref('');
const router = useRouter();

// 动态效果状态
const phoneIsFocused = ref(false);
const passwordIsFocused = ref(false);
const switchBtnHover = ref(false);

const isPhoneValid = () => /^1[3-9]\d{9}$/.test(phone.value);
const canLogin = () => isPhoneValid() && password.value.length >= 6 && agree.value;

function validatePhone() {
  if (!phone.value) {
    phoneError.value = '请输入手机号';
  } else if (!isPhoneValid()) {
    phoneError.value = '请输入有效的手机号';
  } else {
    phoneError.value = '';
  }
}

function validatePassword() {
  if (!password.value) {
    passwordError.value = '请输入密码';
  } else if (password.value.length < 6) {
    passwordError.value = '密码不能少于6位';
  } else {
    passwordError.value = '';
  }
}

function handleLogin() {
  phoneError.value = '';
  passwordError.value = '';
  if (!isPhoneValid()) {
    phoneError.value = '请输入有效的手机号';
    return;
  }
  if (password.value.length < 6) {
    passwordError.value = '密码不能少于6位';
    return;
  }
  if (!agree.value) {
    passwordError.value = '请同意用户协议';
    return;
  }
  // 这里可以调用后端API进行登录
  router.push('/main');
}

const goCodeLogin = () => {
  router.push('/');
};
</script>

<template>
  <div class="login-bg">
    <div class="login-card">
      <h2 class="login-title">THINK · 智慧校园<br />后台管理系统</h2>

      <div class="input-group" :class="{ 'input-focus': phoneIsFocused }">
        <div class="input-label">请输入手机号</div>
        <input v-model="phone" placeholder="请输入11位手机号" class="custom-input" maxlength="11" type="text"
          @focus="phoneIsFocused = true" @blur="() => { phoneIsFocused = false; validatePhone(); }" />
        <div class="input-underline"></div>
        <div class="input-active-underline" :class="{ 'underline-active': phoneIsFocused }"></div>
      </div>
      <div class="error" v-if="phoneError">{{ phoneError }}</div>

      <div class="input-group" :class="{ 'input-focus': passwordIsFocused }">
        <div class="input-label">请输入密码</div>
        <input v-model="password" placeholder="请输入密码" class="custom-input" type="password"
          @focus="passwordIsFocused = true" @blur="() => { passwordIsFocused = false; validatePassword(); }" />
        <div class="input-underline"></div>
        <div class="input-active-underline" :class="{ 'underline-active': passwordIsFocused }"></div>
      </div>
      <div class="error" v-if="passwordError">{{ passwordError }}</div>

      <div class="login-agree">
        <el-checkbox v-model="agree" />
        <span>我已阅读并同意 <a href="#" style="color:#1da1f2">《THINK智慧校园用户协议》</a></span>
      </div>

      <div class="button-group">
        <el-button type="primary" class="login-btn" :disabled="!canLogin()" @click="handleLogin"
          :class="{ 'btn-active': canLogin() }">
          登录
        </el-button>
        <el-button class="switch-btn" @click="goCodeLogin" :class="{ 'switch-btn-hover': switchBtnHover }"
          @mouseenter="switchBtnHover = true" @mouseleave="switchBtnHover = false">
          切换验证码登录
        </el-button>
      </div>
    </div>
    <div class="login-circle"></div>
  </div>
</template>

<style scoped>
.login-bg {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: #25b4fd;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 0;
}

.login-card {
  background: #fff;
  border-radius: 32px;
  box-shadow: 0 12px 48px rgba(0, 0, 0, 0.12);
  padding: 64px 56px 48px 56px;
  width: 540px;
  max-width: 96vw;
  display: flex;
  flex-direction: column;
  align-items: center;
  z-index: 2;
  transform: translateY(0);
  animation: fadeIn 0.5s ease-out;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.login-title {
  color: #3bb6fd;
  font-size: 40px;
  font-weight: 900;
  text-align: center;
  margin-bottom: 56px;
  line-height: 1.2;
}

.input-group {
  width: 100%;
  margin-bottom: 36px;
  position: relative;
  transition: all 0.3s ease;
}

.input-focus {
  transform: translateY(-2px);
}

.input-label {
  color: #bfbfbf;
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 10px;
  transition: color 0.3s ease;
}

.input-focus .input-label {
  color: #3bb6fd;
}

.custom-input {
  width: 100%;
  border: none;
  outline: none;
  font-size: 22px;
  font-weight: normal;
  padding: 8px 0;
  transition: all 0.3s ease;
  background: transparent;
}

.input-underline {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 1px;
  background-color: #e0e0e0;
  transition: all 0.3s ease;
}

.input-active-underline {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 0;
  height: 2px;
  background-color: #3bb6fd;
  transition: all 0.3s ease;
}

.underline-active {
  width: 100%;
}

.login-agree {
  display: flex;
  align-items: center;
  margin-bottom: 36px;
  width: 100%;
  transition: all 0.3s ease;
}

.login-agree:hover {
  transform: translateX(5px);
}

.custom-checkbox :deep(.el-checkbox__input.is-checked .el-checkbox__inner) {
  background-color: #3bb6fd;
  border-color: #3bb6fd;
}

.button-group {
  display: flex;
  flex-direction: column;
  width: 100%;
  gap: 20px;
}

.login-btn {
  width: 100%;
  height: 64px;
  border-radius: 32px;
  font-size: 26px;
  font-weight: bold;
  background: linear-gradient(90deg, #25b4fd 0%, #3bb6fd 100%);
  box-shadow: 0 6px 24px rgba(59, 182, 253, 0.18);
  border: none;
  letter-spacing: 2px;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.login-btn:not(:disabled):hover {
  transform: translateY(-3px);
  box-shadow: 0 10px 24px rgba(59, 182, 253, 0.25);
}

.login-btn:not(:disabled):active {
  transform: translateY(1px);
}

.btn-active {
  animation: buttonGlow 2s infinite;
}

@keyframes buttonGlow {
  0% {
    box-shadow: 0 6px 24px rgba(59, 182, 253, 0.18);
  }

  50% {
    box-shadow: 0 6px 30px rgba(59, 182, 253, 0.4);
  }

  100% {
    box-shadow: 0 6px 24px rgba(59, 182, 253, 0.18);
  }
}

.login-btn:disabled {
  background: #b0dfff;
  opacity: 0.7;
}

.switch-btn {
  width: 100%;
  height: 64px;
  border-radius: 32px;
  background: #f5f5f5;
  color: #b0b0b0;
  border: none;
  font-size: 26px;
  font-weight: bold;
  box-shadow: 0 6px 24px rgba(59, 182, 253, 0.10);
  letter-spacing: 2px;
  transition: all 0.3s ease;
}

.switch-btn:hover {
  transform: translateY(-2px);
}

.switch-btn-hover {
  background: #f0f0f0;
  color: #909090;
}

.login-circle {
  position: absolute;
  left: 80px;
  top: 120px;
  width: 220px;
  height: 220px;
  border-radius: 50%;
  border: 16px solid rgba(255, 255, 255, 0.2);
  box-sizing: border-box;
  z-index: 1;
  animation: float 6s ease-in-out infinite;
}

@keyframes float {
  0% {
    transform: translateY(0px);
  }

  50% {
    transform: translateY(-20px);
  }

  100% {
    transform: translateY(0px);
  }
}

.error {
  color: #ff4d4f;
  font-size: 15px;
  text-align: left;
  margin-top: -28px;
  margin-bottom: 16px;
  width: 100%;
}
</style>
